﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Intellisense
{
    public static class MSBuildMatch
    {
        public static List<TextSpan> GetMatchedSpans(string target, string pattern)
        {
            if (string.IsNullOrWhiteSpace(target))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(pattern))
                throw new ArgumentNullException();

            MatchCollection matches = GetMatches(target, pattern, 0);
            if (matches == null || matches.Count <= 0)
                return null;

            List<TextSpan> result = new List<TextSpan>();
            foreach (Match match in matches)
            {
                TextSpan span = new TextSpan { StartPosition = match.Index, EndPosition = match.Index + match.Value.Length };
                result.Add(span);
            }
            return result;
        }

        public static bool IsExist(string target, string pattern)
        {
            if (string.IsNullOrWhiteSpace(target))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(pattern))
                throw new ArgumentNullException();

            Regex rg = new Regex(pattern, RegexOptions.Singleline);
            Match match = rg.Match(target);
            return match.Success;
        }

        public static bool IsSelectedBetweenLastMatch(string target, string pattern, int currentPosition)
        {
            if (string.IsNullOrWhiteSpace(target))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(pattern))
                throw new ArgumentNullException();

            bool result = false;
            Match match = FindLastMatch(target, pattern, currentPosition,false);
            if (match != null)
                result = true;

            return result;
        }

        public static Match FindLastMatch(string target, string pattern, int currentPosition, bool isContainEnd)
        {
            if (string.IsNullOrWhiteSpace(target))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(pattern))
                throw new ArgumentNullException();

            Match result = null;
            MatchCollection matches = GetMatches(target, pattern, 0);
            if (matches != null && matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    int start = m.Index;
                    int end = start + m.Value.Length;
                    if (!isContainEnd)
                    {
                        if (start < currentPosition && currentPosition < end)
                        {
                            result = m;
                            break;
                        }
                    }
                    else
                    {
                        if (start < currentPosition && currentPosition <= end)
                        {
                            result = m;
                            break;
                        }
                    }
                }
                //Match match = matches[matches.Count - 1];
                //int start = match.Index;
                //int end = start + match.Value.Length;
                //if (start < currentPosition && currentPosition <= end)
                //    result = match;
            }
            return result;
        }

        public static MatchCollection GetMatches(string target, string pattern, int startat)
        {
            if (string.IsNullOrWhiteSpace(target))
                throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(pattern))
                throw new ArgumentNullException();

            Regex rg = new Regex(pattern, RegexOptions.Singleline);
            return rg.Matches(target, startat);
        }

    }
}
